package com.javaweb.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.admin.entity.KDLCustomer;
import com.javaweb.admin.entity.KDLCustomerLog;
import com.javaweb.admin.entity.KDLSupplier;
import com.javaweb.admin.entity.KDLSupplierLog;
import com.javaweb.admin.mapper.KDLCustomerMapper;
import com.javaweb.admin.mapper.KDLSupplierMapper;
import com.javaweb.admin.query.KDLCustomerQuery;
import com.javaweb.admin.query.KDLSupplierQuery;
import com.javaweb.admin.service.IKDLSupplierService;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class KDLSupplierServiceImpl extends BaseServiceImpl<KDLSupplierMapper, KDLSupplier> implements IKDLSupplierService {
    @Autowired
    private KDLSupplierMapper kdlSupplierMapper;

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getList(BaseQuery query) {
        KDLSupplierQuery customerQuery = (KDLSupplierQuery) query;
        // 查询条件
        QueryWrapper<KDLSupplier> queryWrapper = new QueryWrapper<>();
        // 会员等级名称
        if (!StringUtils.isEmpty(customerQuery.getSuppliercode())) {
            queryWrapper.and(wq->{wq.like("suppliercode", customerQuery.getSuppliercode()).or().like("suppliername", customerQuery.getSuppliercode());});
        }
        if (!StringUtils.isEmpty(customerQuery.getAddress())) {
            queryWrapper.like("address", customerQuery.getAddress());
        }
        if (!StringUtils.isEmpty(customerQuery.getStatus())) {
            queryWrapper.eq("status", customerQuery.getStatus());
        }
        if (!StringUtils.isEmpty(customerQuery.getFcbz())) {
            queryWrapper.eq("fcbz", customerQuery.getFcbz());
        }
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByDesc("ncts");

        // 查询分页数据
        IPage<KDLSupplier> page = new Page<>(customerQuery.getPage(), customerQuery.getLimit());
        IPage<KDLSupplier> pageData = kdlSupplierMapper.selectPage(page, queryWrapper);
        return JsonResult.success(pageData);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public List<KDLSupplierLog> getNCSupplierLogList(String customercode) {
        return kdlSupplierMapper.GetNCSupplierLogList(customercode);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getSupplierList(BaseQuery query) {
        KDLSupplierQuery customerQuery = (KDLSupplierQuery) query;
        IPage<KDLSupplier> page = new Page<>(customerQuery.getPage(), customerQuery.getLimit());
        IPage<KDLSupplier> pageData = kdlSupplierMapper.getSupplierList(page, customerQuery);
        return JsonResult.success(pageData);
    }
}
